package com.cn.kehong.service;

import static com.cn.kehong.service.OeeServiceImpl.DateRangeExample.getDateRanges;

import com.cn.kehong.api.IOeeService;
import com.cn.kehong.api.ISysSupplierService;
import com.cn.kehong.conf.Config;
import com.cn.kehong.dao.*;
import com.cn.kehong.domain.*;
import com.cn.kehong.utils.*;
import com.google.common.collect.Lists;
import java.text.SimpleDateFormat;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;
import java.util.*;

import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.servlet.http.HttpServletResponse;

@Service
public class SysSupplierServiceImpl implements ISysSupplierService {

    @Autowired
    private SysSupplierMapper sysSupplierMapper;
    @Autowired
    private Config config;

    @Override
    public List<SysSupplier> getSupplier(QueryForm queryForm) {

        int count = sysSupplierMapper.getSupplierByCount(queryForm);
        if(count == 0){
            return Lists.newArrayList();
        }
        List<SysSupplier> list = sysSupplierMapper.getSupplier(queryForm);

        if (list.size()>0){
            list.get(0).setCount(count);
        }
        return list;
    }

    @Override
    public String addSupplier(SysSupplier sysSupplier) throws Exception {
        String result = this.supplierAddChange(sysSupplier);
        if (StringUtils.isNotEmpty(result)){
            return result;
        }
        String id = CommonUtil.generateRandomNum("sysSupplier_");
        sysSupplier.setId(id);
        sysSupplier.setCreateTime(new Date());
        //供方代码校验
        SysSupplierExample example = new SysSupplierExample();
        example.createCriteria().andSupplierCodeEqualTo(sysSupplier.getSupplierCode()).andStatusNotEqualTo("-1");
        int supplierCode = sysSupplierMapper.countByExample(example);
        if (supplierCode>=1){
            return "已经存在相同的供方代码！";
        }
        //供方名称校验
        SysSupplierExample example1 = new SysSupplierExample();
        example1.createCriteria().andSupplierNameEqualTo(sysSupplier.getSupplierName()).andStatusNotEqualTo("-1");
        int supplierName = sysSupplierMapper.countByExample(example1);
        if (supplierName>=1){
            return "已经存在相同的供方名称！";
        }
        //简称校验
        SysSupplierExample example2 = new SysSupplierExample();
        example2.createCriteria().andReferredToAsEqualTo(sysSupplier.getReferredToAs()).andStatusNotEqualTo("-1");
        int referredToAs = sysSupplierMapper.countByExample(example2);
        if (referredToAs>=1){
            return "已经存在相同的简称！";
        }

        int count = sysSupplierMapper.insertSelective(sysSupplier);
        if (count < 1) {
            return "新增供方信息失败！";
        }
        return CommonUtil.outStr(count);
    }

    @Override
    public String deleteSupplier(QueryForm queryForm) throws Exception {
        List<String> ids = queryForm.getIds();
        if (CollectionsUtil.isEmpty(ids)){
            return "请选中要删除的供方信息！";
        }
        //TODO 设备表可能关联其他表，判断是否可以删除

        SysSupplier sysSupplier = new SysSupplier();
        sysSupplier.setStatus(Constants.DEL_STATUS);
        SysSupplierExample example = new SysSupplierExample();
        example.createCriteria().andIdIn(ids);
        int count = sysSupplierMapper.updateByExampleSelective(sysSupplier,example);

        return CommonUtil.outStr(count);
    }

    @Override
    public String updateOeeOrganization(SysSupplier sysSupplier)  throws Exception {
        String id = sysSupplier.getId();

        if (StringUtils.isEmpty(id)){
            return "id is null";
        }

        String result = this.supplierAddChange(sysSupplier);

        //判断修改的数据是否满足字段的必填条件
        if (StringUtils.isNotEmpty(result)){
            return result;
        }
        //供方代码校验
        if (this.countCode(sysSupplier.getSupplierCode(),id) > 0){
            return "已经存在相同的供方代码";
        }
        //供方名称校验
        if (this.countName(sysSupplier.getSupplierName(),id) > 0){
            return "已经存在相同的供方名称";
        }
        //简称校验
        if (this.countReferredToAs(sysSupplier.getReferredToAs(),id) > 0){
            return "已经存在相同的简称";
        }
        SysSupplierExample examples = new SysSupplierExample();
        examples.createCriteria().andIdEqualTo(id).andStatusNotEqualTo(Constants.DEL_STATUS);

        int count = sysSupplierMapper.updateByExampleSelective(sysSupplier,examples);
        return CommonUtil.outStr(count);
    }


    @Override
    public void exportSupplier(QueryForm queryForm, HttpServletResponse response) throws Exception {
        List<SysSupplier> list = sysSupplierMapper.getSupplierByExcel(queryForm);
        if (CollectionsUtil.isEmpty(list)){
            return;
        }
        Map<String,Object> result = Maps.newHashMap();
        result.put("check", list);
        String fileName = CommonUtil.generateRandomNum("supplier-")+".xlsx";
        String filePath = config.filePath+fileName;
        TemplateExcelUtils.downLoadExcel(filePath,"供方信息.xlsx",result,"supplier_check.xlsx", response);

    }

    @Override
    public Map<String, SysSupplier> getSysSupplierByCode() throws Exception {
        SysSupplierExample example = new SysSupplierExample();
        example.createCriteria().andStatusNotEqualTo("-1");
        List<SysSupplier> sysSuppliers = sysSupplierMapper.selectByExample(example);

        Map<String,SysSupplier> maps = Maps.newHashMap();
        for(SysSupplier sysSupplier : sysSuppliers){
            maps.put(sysSupplier.getSupplierCode(),sysSupplier);
        }
        return maps;
    }

    private String supplierAddChange(SysSupplier sysSupplier) {
        String result = "";
        if (StringUtils.isBlank(sysSupplier.getSupplierCode())){
            result = "请输入供方代码";
            return result;
        }
        if (StringUtils.isBlank(sysSupplier.getSupplierName())){
            result = "请输入供方名称";
            return result;
        }
        if (StringUtils.isBlank(sysSupplier.getReferredToAs())){
            result = "请输入简称";
            return result;
        }
        if (ObjectUtils.isEmpty(sysSupplier.getMaterialNumber())){
            result = "请输入物料品类数";
            return result;
        }
        if (StringUtils.isBlank(sysSupplier.getStatus())){
            result = "请输入状态";
            return result;
        }
        return result;
    }

    private int countCode(String code,String id) {
        SysSupplierExample example = new SysSupplierExample();
        SysSupplierExample.Criteria criteria = example.createCriteria();

        criteria.andStatusNotEqualTo(Constants.DEL_STATUS).andSupplierCodeEqualTo(code);
        if (StringUtils.isNotEmpty(id)){
            criteria.andIdNotEqualTo(id);
        }
        int count = sysSupplierMapper.countByExample(example);
        return count;
    }


    private int countName(String name,String id) {
        SysSupplierExample example = new SysSupplierExample();
        SysSupplierExample.Criteria criteria = example.createCriteria();
        criteria.andStatusNotEqualTo(Constants.DEL_STATUS).andSupplierNameEqualTo(name);
        if (StringUtils.isNotEmpty(id)){
            criteria.andIdNotEqualTo(id);
        }
        int count = sysSupplierMapper.countByExample(example);
        return count;

    }
    private int countReferredToAs(String ReferredToAs,String id) {
        SysSupplierExample example = new SysSupplierExample();
        SysSupplierExample.Criteria criteria = example.createCriteria();
        criteria.andStatusNotEqualTo(Constants.DEL_STATUS).andReferredToAsEqualTo(ReferredToAs);
        if (StringUtils.isNotEmpty(id)){
            criteria.andIdNotEqualTo(id);
        }
        int count = sysSupplierMapper.countByExample(example);
        return count;
    }

}
